﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class TaskController : Controller
    {
        Model1 model = new Model1();
        // GET: Task
        public ActionResult Index()
        {
            var list = model.Task.ToList();
            return View(list);
        }
        //添加
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                task.AddTime = DateTime.Now;
                task.TaskState = 1;
                task.FinishedTime = null;
                model.Task.Add(task);
                var result = model.SaveChanges();
                if (result>0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
        }
        //修改
        public ActionResult Edit(int?id)
        {
            if (id==null)
            {
                return RedirectToAction("Index");
            }
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task==null)
            {
                return RedirectToAction("Index");
            }
            return View(task);
        }
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {

                var temp = model.Task.FirstOrDefault(p => p.TaskID ==task.TaskID);
                if (temp == null)
                {
                    return RedirectToAction("Index");
                }
                temp.TaskPriority = task.TaskPriority;
                temp.TaskState = task.TaskState;
                temp.Content = task.Content;
                temp.TaskState = task.TaskState;
                temp.uid = task.uid;
                var result = model.SaveChanges();
                if (result > 0)
                {
                    return RedirectToAction("Index");
                }
                }
                 return View(task);
            }


        //详情
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return RedirectToAction("Index");
            }
            return View(task);
        }

        //删除
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id==null)
            {
                return Json(false);
            }
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return Json(false);
            }
            model.Task.Remove(task);
            var res = model.SaveChanges()>0;
            return Json(res);
        }
    }

}